package com.centaline.androidsalesblog.rx.request;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.usercenter.VerificationCodeApi;
import com.centaline.androidsalesblog.model.responseobject.VerificationCodeRo;
import com.centanet.framework.interfaces.IResponseListener;

import rx.Observable;
import rx.Subscriber;

/**
 * 检查验证码
 * Created by guilin on 16/3/18.
 */
final class RequestVerificationCodeOnSubscribe implements Observable.OnSubscribe<VerificationCodeRo> {

    private final Class<?> tag;
    private final String mobile;
    private final String code;

    public RequestVerificationCodeOnSubscribe(Class<?> tag, String mobile, String code) {
        this.tag = tag;
        this.mobile = mobile;
        this.code = code;
    }

    @Override
    public void call(final Subscriber<? super VerificationCodeRo> subscriber) {
        VerificationCodeApi verificationCodeApi = new VerificationCodeApi(tag, VerificationCodeRo.class, new IResponseListener<VerificationCodeRo>() {
            @Override
            public void onResponse(VerificationCodeRo verificationCodeRo) {
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onNext(verificationCodeRo);
                }
                subscriber.onCompleted();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                subscriber.onError(error);
            }
        });
        verificationCodeApi.setMobile(mobile);
        verificationCodeApi.setCode(code);
        verificationCodeApi.startRequest();
    }
}
